package net.ifilm.util;

import net.ifilm.common.Constants;

import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    private static final String PLAY_URL_PATTERN = "(.*)\\+\\+(.*)";

    public static String adapterWord(String clearStr, String targetStr) {
        if (targetStr == null || "".equals(targetStr)) return "未知";
        targetStr = targetStr.trim();
        if (clearStr == null || "".equals(clearStr)) return targetStr;
        String resultStr = targetStr.replaceAll(clearStr, "");
        return resultStr;
    }

    public static int matchFilmType(String filmTypeName) {
        if (filmTypeName == null || "".equals(filmTypeName))
            throw new IllegalArgumentException("filmTypename argument is invalid");

        if ("动画片".equals(filmTypeName)) { return Constants.FILM_TYPE_CARTOON; }
        else if ("动作片".equals(filmTypeName)) { return Constants.FILM_TYPE_ACTION; }
        else if ("喜剧片".equals(filmTypeName)) { return Constants.FILM_TYPE_FUNNY; }
        else if ("爱情片".equals(filmTypeName)) { return Constants.FILM_TYPE_LOVEY; }
        else if ("科幻片".equals(filmTypeName)) { return Constants.FILM_TYPE_SIENCE; }
        else if ("恐怖片".equals(filmTypeName)) { return Constants.FILM_TYPE_TERRIFY; }
        else if ("战争片".equals(filmTypeName)) { return Constants.FILM_TYPE_WAR; }
        else if ("故事片".equals(filmTypeName)) { return Constants.FILM_TYPE_STORY; }
        else { return Constants.FILM_TYPE_STORY; }
    }

    public static int getFilmType(String filmTypeName) {
        if ("未知".equals(filmTypeName)) return 1;    // 类型名称为空时默认为电影
        else if ("综艺节目".equals(filmTypeName)) return 3;
        else if ("动画片".equals(filmTypeName)) return 4;
        else if ("海外剧".equals(filmTypeName) || "日韩剧".equals(filmTypeName)
                || "欧美剧".equals(filmTypeName) || "港台剧".equals(filmTypeName)
                || "国产剧".equals(filmTypeName)) return 2;
        else return 1;
    }

    public static int matchRegion(String regionName) {
        if ("日本".equals(regionName) || "日韩".equals(regionName) || "韩国".equals(regionName)) { return 1; }
        else if ("欧美".equals(regionName) || "印度".equals(regionName) || "美国".equals(regionName)) { return 4; }
        else if ("台湾".equals(regionName) || "港台".equals(regionName) || "香港".equals(regionName)) { return 2; }
        else if ("大陆".equals(regionName) || "东南亚".equals(regionName)) { return 3; }
        else { return 4; }
    }

    public static String[] matchFilmActors(String filmActors) {
        if ("不详".equals(filmActors) || "未填".equals(filmActors)
                || "未知".equals(filmActors) || "".equals(filmActors) || "未填写".equals(filmActors)
                || null == filmActors)
            return new String[]{ "未知" };
        if (filmActors.indexOf(' ') == -1) return new String[]{ filmActors };
        return filmActors.split(" ");
    }

    public static Matcher extractPlayNetPath(String targetStr) {
        if (targetStr == null || "".equals(targetStr))
            throw new IllegalArgumentException("targetStr argument is invalid");

        Pattern p = Pattern.compile(PLAY_URL_PATTERN);
        Matcher m = p.matcher(targetStr);
        return m;
    }

    public static String generateRandom() {
        return UUID.randomUUID().toString();
    }

    public static String extraKeyword(String pattern, String targetStr) {
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(targetStr);
        if (m.find()) {
            return m.group(1);
        }

        return null;
    }
}
